import requests
from bs4 import BeautifulSoup
import pandas as pd

# https://beautifulsoup.cn/
# https://pypandas.cn/docs/
# 目标网站的URL
url = 'https://gdp.gotohui.com/data-20/1/2024'

# 发送HTTP GET请求
response = requests.get(url)

# 检查请求是否成功
if response.status_code == 200:
    # 解析网页内容
    soup = BeautifulSoup(response.text, 'html.parser')

    # 提取网页的标题
    title = soup.find('title').get_text()

    # 打印标题
    print('网页标题:', title)

    # cityTag = soup.find_all('td', class_='leftx')
    # city = []
    # for item in cityTag:
    #     city.append(item.find(['a']).get_text())
    # print('city:', city)

    table = soup.find('table', class_='table-hover')
    th = table.find_all(['th'])
    td = table.find_all(['td'])
    print('td',td)
    title = []
    city = []
    for item in th:
        title.append(item.get_text())
    # print('title:', title)
    for item in td:
        # print("a",item.find('a'))
        print("td", item)
        # city.append(item.find(['a']).get_text())

    # 创建一个DataFrame
    # data = pd.DataFrame({'网页标题': [title]})

    # 将DataFrame保存到CSV文件
    # data.to_csv('./xlsx/city.csv', index=False)
    # # data.to_csv('./xlsx/city.xlsx', index=False)
    # print('数据已保存到city.csv文件中')


else:
    print('请求失败，状态码:', response.status_code)
